package sample.model;

import javafx.scene.control.CheckBoxTreeItem;
import sample.util.ListCrossoverperation;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.util.*;

/**
 * addby@wutc 20210315
 * 读取服务器基础信息
 * app2	192.168.1.20	root/xxxx default
 * 应用名称 应用ip地址  用户名/密码  自定义部署路径
 */

public class ServerInfo {

    private CheckBoxTreeItem<String> root = new CheckBoxTreeItem<String>("应用");
    private LinkedHashMap<String, String[]> serverMap = new LinkedHashMap<String, String[]>();
    private List<String> ipArrayList = new ArrayList<String>();
    private HashMap<String, String[]> myUseServerMap = new HashMap<String, String[]>();



    //读取csv文件获取应用名称 和服务器地址
    private void ReadServerInfo(String path){
        String serverName = "";//服务器名称
        String user = "";// 用户名
        String pwd = "";// 密码
        String ip = "";// 服务器地址
        String nmonpath = "";//自定义nmon部署路径

        //存储服务器信息到变量中，不需要多次打开文件
        //读取文件
        try {
            //(文件完整路径),编码格式
            BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(path), "utf-8"));//GBK
//                 reader.readLine();//显示标题行,没有则注释掉
//                 System.out.println(reader.readLine());
            String line = null;
            while((line=reader.readLine())!=null){
                String[] arr = new String[5];
                serverName = line.split(",")[0].toString();
                ip = line.split(",")[1].toString();
                user = line.split(",")[2].toString().split("/")[0].toString();
                pwd = line.split(",")[2].toString().split("/")[1].toString();
                nmonpath = line.split(",")[3].toString();
                arr[0] = serverName;
                arr[1] = user;
                arr[2] = pwd;
                arr[3] = nmonpath;
                arr[4] = "1";//1 表示获取消息 0 表示跳过
                ipArrayList.add(ip);
                serverMap.put(ip, arr);
                myUseServerMap.put(ip, arr);
            }
            reader.close();
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    public CheckBoxTreeItem<String> getServerInfo(){
        return root;
    }

    public HashMap<String, String[]> getServerMap() {
        return serverMap;
    }


    public ServerInfo(String path){
        //System.out.println("ServerInfo 无参构造");
        //读取服务器文件
        ReadServerInfo(path);

        //修改默认状态为勾选
        root.setSelected(true);

        //显示服务器的数量
        root.setValue("应用( "+ serverMap.size() +" )");

        //获取serverMap 到CheckBoxTreeItem
        CheckBoxTreeItem<String> c1 = null;
        String oldstring = null;

        //checkboxtree赋值
        for (HashMap.Entry<String, String[]> hs:serverMap.entrySet()) {
            //System.out.println(hs.getKey()+"---"+hs.getValue()[0]+oldstring);

            CheckBoxTreeItem<String> checkBoxTreeItem2 = new CheckBoxTreeItem<String>(hs.getKey());
            checkBoxTreeItem2.setSelected(true);//设定默认为选中状态
            if (!hs.getValue()[0].equals(oldstring)){
                CheckBoxTreeItem<String> checkBoxTreeItem1 = new CheckBoxTreeItem<String>(hs.getValue()[0]);
                checkBoxTreeItem1.setExpanded(true);//设定默认为展示状态
                checkBoxTreeItem1.setSelected(true);//设定默认为选中状态
                c1 = checkBoxTreeItem1;
                oldstring = hs.getValue()[0];
                c1.getChildren().add(checkBoxTreeItem2);
                root.getChildren().add(c1);
            }else {
                c1.getChildren().add(checkBoxTreeItem2);
            }

        }

//        for (int i=0;i<ipArrayList.size();i++){
//            //System.out.println(hs.getKey()+"---"+hs.getValue()[0]+oldstring);
//
//            CheckBoxTreeItem<String> checkBoxTreeItem2 = new CheckBoxTreeItem<String>(hs.getKey());
//            checkBoxTreeItem2.setSelected(true);//设定默认为选中状态
//            if (!hs.getValue()[0].equals(oldstring)){
//                CheckBoxTreeItem<String> checkBoxTreeItem1 = new CheckBoxTreeItem<String>(hs.getValue()[0]);
//                checkBoxTreeItem1.setExpanded(true);//设定默认为展示状态
//                checkBoxTreeItem1.setSelected(true);//设定默认为选中状态
//                c1 = checkBoxTreeItem1;
//                oldstring = hs.getValue()[0];
//                c1.getChildren().add(checkBoxTreeItem2);
//                root.getChildren().add(c1);
//            }else {
//                c1.getChildren().add(checkBoxTreeItem2);
//            }
//        }


    }

    public HashMap<String, String[]> setServerMap(List<String> secondArrayList) {
       // System.out.println("1   "+ipArrayList.toString());
        //System.out.println("1要监控的   "+secondArrayList.toString());
        //差集运算
        List<String> defectList = new ArrayList<String>();//差集List
        defectList = ListCrossoverperation.receiveDefectList(ipArrayList, secondArrayList);
        //System.out.println("===================差集===================");
        //System.out.println("0不监控的   "+defectList.toString());
        myUseServerMap.clear();
        //设定差集内的
//        while(defectIterator.hasNext()) {
//            System.out.print(defectIterator.next()+" --  ");
//        }
        //secondArrayList 都是要监控的 1
        //defectList 都是不要监控的 0
        //监控属性
        for (int i =0;i < ipArrayList.size();i++){
            if (secondArrayList.contains(ipArrayList.get(i))){
                //设定为监控
                serverMap.get(ipArrayList.get(i))[4]="1";
                myUseServerMap.put(ipArrayList.get(i),serverMap.get(ipArrayList.get(i)));
            }else {
                //设定为不监控
                serverMap.get(ipArrayList.get(i))[4]="0";
            }
        }

        return serverMap;
    }

    public HashMap<String, String[]> getMyUseServerMap() {
        return myUseServerMap;
    }



}
